package com.tsingtree.modular.controller.view;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanScene;
import com.tsingtree.modular.enums.SceneTypeEnum;
import com.tsingtree.modular.model.params.NanSceneParam;
import com.tsingtree.modular.service.NanSceneService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


/**
 * 玩法控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanScene")
public class NanSceneController extends BaseController {

    private String PREFIX = "/nanScene";

    @Autowired
    private NanSceneService nanSceneService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanScene.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanScene_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanScene_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanSceneParam nanSceneParam) {
        this.nanSceneService.add(nanSceneParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanSceneParam nanSceneParam) {
        this.nanSceneService.update(nanSceneParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @Permission
    @ResponseBody
    public ResponseData delete(NanSceneParam nanSceneParam) {
        this.nanSceneService.delete(nanSceneParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSceneParam nanSceneParam) {
        NanScene detail = this.nanSceneService.getById(nanSceneParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSceneParam nanSceneParam) {
        return this.nanSceneService.findPageBySpec(nanSceneParam);
    }

    @ResponseBody
    @RequestMapping("/listStruct")
    public Object listStruct() {
        List<NanScene> list = this.nanSceneService.list(new QueryWrapper<NanScene>().orderByAsc("type", "id"));
        Map<String, List<NanScene>> map = new TreeMap<>();
        list.forEach((nanScene -> {
            String name = SceneTypeEnum.getAll().get(nanScene.getType());
            List<NanScene> current = map.getOrDefault(name, new ArrayList<>());
            current.add(nanScene);
            map.put(name, current);
        }));
//        JSONArray jsonArray = new JSONArray();
//        map.forEach((key, value) -> {
//            Map<String, Object> m = new HashMap<>();
//            m.put("key", key);
//            m.put("value", value);
//            jsonArray.add(m);
//        });
        return ResponseData.success(map);
    }

}


